﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH;
using QLBH.Model.Controller;
using QLBH.DataModel;
using Module.Category.Model;
using QLBH.DTO;
using System.Transactions;

namespace Module.Category
{
    public partial class StaffManagement : ViewBase
    {
        private static StaffManagement instance;

        public static StaffManagement Instance
        {
            get 
            {
                if (instance==null)
                {
                    instance = new StaffManagement();
                }
                return StaffManagement.instance; 
            }
            
        }
        public StaffManagement()
        {
            InitializeComponent();
        }       

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            StaffInfoDialog addStaffdlg = new StaffInfoDialog();
            addStaffdlg.DialogType = eDialogType.DL_ADD;
            if (addStaffdlg.ShowDialog()==DialogResult.OK)
            {
                LoadStaffList();
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            EditStaff();
        }
        private void EditStaff()
        {
            StaffInfoDialog editStaffInfo = new StaffInfoDialog();
            editStaffInfo.DialogType = eDialogType.DL_EDIT;
            StaffInfoModel staffModel=dataGridViewX1.CurrentRow.DataBoundItem as StaffInfoModel;

            NhanVienDTO currentStaff=StaffController.Instance.FindBy( st => st.Id==staffModel.Id).FirstOrDefault().ToDTO();

            editStaffInfo.Staff=currentStaff;

            if (editStaffInfo.ShowDialog()==DialogResult.OK)
            {
                LoadStaffList();
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            try
            {
                using (TransactionScope tran = new TransactionScope())
                {
                    StaffInfoModel staffModel = dataGridViewX1.CurrentRow.DataBoundItem as StaffInfoModel;

                    StaffController.Instance.Delete(nv => nv.Id == staffModel.Id);

                    AccountController.Instance.Delete(ac => ac.NhanVienId == staffModel.Id);

                    tran.Complete();
                }
                LoadStaffList();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadStaffList();
        }
        private void LoadStaffList()
        {
            List<NhanVien> staffs=StaffController.Instance.GetAll().ToList();
            if (staffs!=null)
            {
                List<StaffInfoModel> listStaff = new List<StaffInfoModel>();
                foreach (NhanVien staff in staffs)
                {
                    StaffInfoModel staffInfo = new StaffInfoModel();
                    staffInfo.Id = staff.Id;
                    staffInfo.HoTen = staff.TenNV;
                    staffInfo.DiaChi = staff.DienChi;
                    staffInfo.Email = staff.Email;
                    staffInfo.SDT = staff.SDT;
                    staffInfo.MucLuong = staff.MucLuong;
                    staffInfo.ChiNhanh = staff.ChiNhanh;
                    staffInfo.NganHang = staff.NganHang;
                    staffInfo.STK = staff.STK;
                    

                    ChucVu currentPos=PositionController.Instance.FindBy(cv => cv.Id==staff.ChucVuId).FirstOrDefault();
                    if (currentPos!=null)
                    {
                        staffInfo.ChucVu = currentPos.TenCV;
                    }                   

                    listStaff.Add(staffInfo);
                }

                dataGridViewX1.DataSource = listStaff;
                dataGridViewX1.Columns["Id"].HeaderText = "ID";
                dataGridViewX1.Columns["HoTen"].HeaderText = "Họ tên";
                dataGridViewX1.Columns["DiaChi"].HeaderText = "Địa chỉ";
                dataGridViewX1.Columns["SDT"].HeaderText = "SĐT";
                dataGridViewX1.Columns["Email"].HeaderText = "Email";
                dataGridViewX1.Columns["MucLuong"].HeaderText = "Mức lương";
                dataGridViewX1.Columns["ChucVu"].HeaderText = "Chức vụ";
                dataGridViewX1.Columns["STK"].HeaderText = "STK";
                dataGridViewX1.Columns["NganHang"].HeaderText = "Ngân hàng";
                dataGridViewX1.Columns["ChiNhanh"].HeaderText = "Chi nhánh";
            }
        }

        private void StaffManagement_Load(object sender, EventArgs e)
        {
            LoadStaffList();
        }

        private void dataGridViewX1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditStaff();
        }

        private void dataGridViewX1_DataSourceChanged(object sender, EventArgs e)
        {
            if (dataGridViewX1.RowCount == 0)
            {
                buttonEdit.Enabled = false;
                buttonDelete.Enabled = false;
            }
            else
            {
                buttonEdit.Enabled = true;
                buttonDelete.Enabled = true;
            }
        }
    }
}
